package com.ruoyi.task.controller;


import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.task.domain.TaskMediaResp;
import com.ruoyi.task.domain.entity.DataTaskEntity;
import com.ruoyi.task.service.IDataTaskService;
import io.swagger.annotations.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 测试任务Controller
 */
@RestController
@RequestMapping("/data/task")
@Api(value="测试任务",tags = "测试任务接口")
public class DataTaskController extends BaseController {

    @Resource
    private IDataTaskService dataTaskService;

    /**
     * 查询测试任务列表
     * <p>
     * 本部门 and  (本人参与的)
     */
    @ApiOperation(value="本部门和(本人参与的)测试任务列表")
    @GetMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "pageNum", value = "页数", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "pageSize", value = "每页记录数", required = true, dataType = "String"),
    })
    public TableDataInfo list(DataTaskEntity dataTaskEntity) {
        startPage();
        List<DataTaskEntity> dataTaskEntities =  dataTaskService.selectDataTaskList(dataTaskEntity, isAdmin(),isPersonal());
        return getDataTable(dataTaskEntities);
    }


    /**
     * 根据任务id查询
     * @param dataTaskId
     * @return
     */
    @PreAuthorize("@ss.hasPermi('data:task:query')")
    @GetMapping(value = "/{dataTaskId}")
    @ApiOperation(value="根据任务id查询")
    @ApiImplicitParam(paramType="query", name = "dataTaskId", value = "任务id", required = true, dataType = "Long")
    public AjaxResult getInfo(@PathVariable("dataTaskId") Long dataTaskId) {
        DataTaskEntity dataTaskEntity = dataTaskService.selectDataTaskByDataTaskId(dataTaskId);
        return success(dataTaskEntity);
    }

    /**
     * 首页根据月份获取柱状图
     * @param dataLength
     * @return
     */
    @GetMapping(value = "/getChartInfo/{dataLength}")
    @ApiOperation(value="首页根据月份获取柱状图")
    @ApiImplicitParam(paramType="query", name = "dataLength", value = "月份长度", required = true, dataType = "Integer")
    public AjaxResult getChartInfo(@PathVariable("dataLength") Integer dataLength) {
        return success(dataTaskService.selectDataTaskByDate(dataLength));
    }

    /**
     * 首页根据具体日期范围获取不同任务类型柱状图
     * @param startTime, endTime
     * @return
     */
    @GetMapping(value = "/getTypeInfo/{startTime}/{endTime}")
    @ApiOperation(value="首页根据具体日期范围获取不同任务类型柱状图")
    public AjaxResult getChartInfo(@PathVariable("startTime") String startTime, @PathVariable("endTime") String endTime) {
        return success(dataTaskService.selectDataTaskByRangeDate(startTime, endTime));
    }



    @PreAuthorize("@ss.hasPermi('data:task:edit')")
    @Log(title = "", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value="修改测试任务")
    public AjaxResult edit(@ApiParam(name="退样登记",value="传入json格式",required=true)@RequestBody DataTaskEntity dataTaskEntity) {
        return toAjax(dataTaskService.updateDataTask(dataTaskEntity));
    }

    /**
     * 删除测试任务
     */
    @PreAuthorize("@ss.hasPermi('data:task:remove')")
    @Log(title = "测试任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dataTaskIds}/{taskType}")
    @ApiOperation(value="删除测试任务")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "dataTaskIds", value = "任务ids", required = true, dataType = "Long"),
            @ApiImplicitParam(paramType="query", name = "taskType", value = "任务类型", required = true, dataType = "String"),
    })
    public AjaxResult remove(@PathVariable Long[] dataTaskIds, @PathVariable String taskType) throws InterruptedException {
        return toAjax(dataTaskService.deleteDataTaskByDataTaskIds(dataTaskIds,taskType));
    }

    /**
     * 查询任务媒体资料
     */
    @PreAuthorize("@ss.hasPermi('data:task:query')")
    @GetMapping(value = "/media/{dataTaskId}")
    @ApiOperation(value="查询任务媒体资料")
    @ApiImplicitParam(paramType="query", name = "dataTaskId", value = "任务id", required = true, dataType = "Long")
    public AjaxResult getMedia(@PathVariable("dataTaskId") Long dataTaskId) {
        TaskMediaResp mediaResp = dataTaskService.selectMedia(dataTaskId);
        return success(mediaResp);
    }

}
